package com.ellwood.IPGuard.controller.system;

import com.ellwood.IPGuard.business.system.repository.UmsSysUserRepositoryAdapter;
import com.ellwood.IPGuard.common.core.basic.ControllerFactory;
import com.ellwood.IPGuard.common.core.enums.HttpCodeEnum;
import com.ellwood.IPGuard.common.core.result.EllPageResult;
import com.ellwood.IPGuard.common.core.result.EllResult;
import com.ellwood.IPGuard.common.core.utils.EllStrUtils;
import com.ellwood.IPGuard.infrastruction.domain.entity.system.UmsSysUserEntity;
import com.ellwood.IPGuard.infrastruction.domain.query.UmsSysUserQuery;
import com.ellwood.IPGuard.infrastruction.domain.vo.system.user.UmsSysUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ProjectName: IPGuard-boot
 * @PackageName: com.ellwood.IPGuard.controller.system
 * @ClassName: UmsSysUserController
 * @Description: // 用户控制器
 * @Author: Ellwood
 * @CreateDate: 2024/10/8 - 下午2:11
 * @Version: V1.0.0
 */

@CrossOrigin
@AllArgsConstructor
@Tag(name = "用户管理", description = "用户管理")
@RestController
@RequestMapping("/api/sys/user")
public class UmsSysUserController extends ControllerFactory<UmsSysUserEntity> {
        
        private final UmsSysUserRepositoryAdapter userRepositoryAdapter;
        
        @GetMapping("/page")
        @Operation(summary = "分页查询用户")
        public EllResult<EllPageResult<UmsSysUserVO>> userPage(@ParameterObject @Valid UmsSysUserQuery userQuery) {
                EllPageResult<UmsSysUserVO> page = userRepositoryAdapter.userPage(userQuery);
                return EllResult.ok(page);
        }
        
        @GetMapping()
        @Operation(summary = "查询所有用户")
        public EllResult<List<UmsSysUserVO>> findAll() {
                List<UmsSysUserVO> list = userRepositoryAdapter.findAll(new UmsSysUserQuery());
                return EllResult.ok(list);
        }
        
        @Operation(summary = "根据ID查询用户")
        @GetMapping("/{id}")
        public EllResult<UmsSysUserVO> getUserById(@PathVariable("id") Long id) {
                UmsSysUserVO user = userRepositoryAdapter.getUserById(id);
                return EllResult.ok(user);
        }
        
        @PostMapping
        @Operation(summary = "新增用户")
        public EllResult<String> userSave(@RequestBody @Valid UmsSysUserVO userVO) {
                if (EllStrUtils.isBlank(userVO.getPassword())) {
                        return EllResult.error(HttpCodeEnum.PASSWORD_NOT_NULL.getMsg());
                }
                
                // 密码加密 TODO 后续加入权限控制再进行这里的处理
                
                // 新增用户
                int result = userRepositoryAdapter.saveUser(userVO);
                
                return result > 0 ? EllResult.ok() : EllResult.error(HttpCodeEnum.ERROR.getMsg());
        }
        
        @PutMapping
        @Operation(summary = "编辑用户")
        public EllResult<String> userUpdate(@RequestBody @Valid UmsSysUserVO userVO) {
                // 如果密码不是空则进行加密处理
                if (EllStrUtils.isBlank(userVO.getPassword())) {
                        userVO.setPassword(null);
                } else {
                        // 密码加密 TODO 后续加入权限控制再进行这里的处理
                }
                
                // 编辑用户
                int result = userRepositoryAdapter.userUpdate(userVO);
                
                return result > 0 ? EllResult.ok() : EllResult.error(HttpCodeEnum.ERROR.getMsg());
        }
        
        @DeleteMapping
        @Operation(summary = "删除用户-批量删除")
        public EllResult<String> userRemove(@RequestBody List<Long> idList) {
                // 首先需要检查当前登录用户是否跟要删除的是一个，如果相同就不允许删除
                // 超级管理员不允许删除
                // TODO 后续加入权限控制再进行这里的处理
                
                int result = userRepositoryAdapter.userRemove(idList);
                return result > 0 ? EllResult.ok() : EllResult.error(HttpCodeEnum.ERROR.getMsg());
        }
        
        @PutMapping("/logicRemove/{userId}")
        @Operation(summary = "删除用户-单个删除")
        public EllResult<String> userLogicRemove(@PathVariable("userId") Long userId) {
                // 首先判断传入的id不是当前登录者id并且也不是超级管理元 TODO 后续加入权限控制再进行这里的处理
                int result = userRepositoryAdapter.userLogicRemove(userId);
                return result > 0 ? EllResult.ok() : EllResult.error(HttpCodeEnum.ERROR.getMsg());
        }
        
        @PostMapping("/importExcel")
        @Operation(summary = "用户数据导入")
        public EllResult<String> userImportExcel(@RequestParam("file")MultipartFile file){
                if (file.isEmpty()) {
                        return EllResult.error("请选择需要上传的文件！！！");
                }
                
                userRepositoryAdapter.userImportExcel(file,"123456");
                return  EllResult.ok() ;
        }
        
        @GetMapping("/exportExcel")
        @Operation(summary = "用户数据导出")
        public void userExport(){
                userRepositoryAdapter.userExport();
        }
        
}
